package com.github.caijh.mall.authorization.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import com.github.caijh.commons.util.Asserts;
import com.github.caijh.framework.core.exception.BizException;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.framework.web.util.RequestUtils;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.constants.Constants;
import com.github.caijh.mall.exception.ExceptionCodeEnum;
import com.github.caijh.mall.system.request.SystemAdminLoginReqBody;
import com.github.caijh.mall.system.response.SystemAdminRespBody;
import com.github.caijh.mall.system.response.SystemGroupDataAdminLoginBannerResponse;
import com.github.caijh.mall.system.service.SystemAdminService;
import com.github.caijh.mall.system.service.SystemConfigService;
import com.github.caijh.mall.system.service.SystemGroupDataService;
import com.github.caijh.mall.validatecode.model.ValidateCode;
import com.github.caijh.mall.validatecode.service.ValidateCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/admin")
@Api(tags = "Admin 平台用户")
public class AdminUserLoginController extends BaseController {

    @Inject
    private SystemAdminService systemAdminService;

    @Inject
    private SystemConfigService systemConfigService;

    @Inject
    private SystemGroupDataService systemGroupDataService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @ApiOperation(value = "AdminUserLogin")
    @PostMapping(value = "/login", produces = "application/json")
    public CommonResult<SystemAdminRespBody> systemAdminLogin(
            @RequestBody @Validated SystemAdminLoginReqBody systemAdminLoginReqBody,
            HttpServletRequest request) {
        // 判断验证码
        ValidateCode validateCode = new ValidateCode(systemAdminLoginReqBody.getKey(), systemAdminLoginReqBody.getCode());
        boolean success = validateCodeService.check(validateCode);
        Asserts.isTrue(success, () -> BizException.of("USER_VALID_CODE_ERROR"));

        SystemAdminRespBody systemAdminRespBody = systemAdminService.login(systemAdminLoginReqBody, RequestUtils.getIp(request));
        if (null == systemAdminRespBody) {
            return CommonResult.failed(ExceptionCodeEnum.FAILED, "login failed");
        }

        if (StringUtils.isNotBlank(systemAdminLoginReqBody.getWxCode())) {
            systemAdminService.bind(systemAdminLoginReqBody.getWxCode(), systemAdminRespBody.getId());
        }
        return CommonResult.success(systemAdminRespBody, "login success");
    }

    @ApiOperation(value = "AdminUserLogout")
    @GetMapping(value = "/logout")
    public CommonResult<SystemAdminRespBody> systemAdminLogout(HttpServletRequest request) throws Exception {
        String token = RequestUtils.extractHeader(request, Constants.HEADER_AUTHORIZATION_KEY);
        systemAdminService.logout(token);
        return CommonResult.success("logout success");
    }

    @ApiOperation(value = "GetAdminUserByToken")
    @GetMapping(value = "/getAdminInfoByToken")
    public CommonResult<SystemAdminRespBody> getAdminInfo(HttpServletRequest request) throws Exception {
        String token = RequestUtils.extractHeader(request, Constants.HEADER_AUTHORIZATION_KEY);
        SystemAdminRespBody systemAdminRespBody = systemAdminService.getInfoByToken(token);

        return CommonResult.success(systemAdminRespBody);
    }

    /**
     * 获取登录页图片
     *
     * @return {@code Map<String, Object>}
     */
    @ApiOperation(value = "获取登录页图片")
    @GetMapping(value = "/getLoginPic")
    public Map<String, Object> getLoginPic() {
        Map<String, Object> map = new HashMap<>();
        //背景图
        map.put("backgroundImage", systemConfigService.getValueByKey(Constants.CONFIG_KEY_ADMIN_LOGIN_BACKGROUND_IMAGE));
        //logo
        map.put("logo", systemConfigService.getValueByKey(Constants.CONFIG_KEY_ADMIN_LOGIN_LOGO));
        //轮播图
        List<SystemGroupDataAdminLoginBannerResponse> bannerList = systemGroupDataService
                .getListByGid(Constants.GROUP_DATA_ID_ADMIN_LOGIN_BANNER_IMAGE_LIST, SystemGroupDataAdminLoginBannerResponse.class);
        map.put("banner", bannerList);
        return map;
    }

    /**
     * 微信登录公共号授权登录
     *
     * @param code    code
     * @param request request
     * @return SystemAdminRespBody
     */
    @ApiOperation(value = "微信登录公共号授权登录")
    @GetMapping(value = "/authorize/login")
    public CommonResult<SystemAdminRespBody> login(@RequestParam(value = "code") String code, HttpServletRequest request) {
        return CommonResult.success(systemAdminService.weChatAuthorizeLogin(code, RequestUtils.getIp(request)));
    }

    /**
     * 解绑微信
     *
     * @return true or false
     */
    @ApiOperation(value = "解绑微信")
    @GetMapping(value = "/unbind")
    public CommonResult<Boolean> bind() {
        return CommonResult.success(systemAdminService.unBind());
    }

}
